#include <stdio.h>
#include <string.h>
#include <mysql/mysql.h>
#include <unistd.h>
#include <pthread.h>

#define MAX_SQL_SIZE 1024

int main(int argc, char **argv)
{
    // 1、创建的mysql的MYSQL
    MYSQL *mysql = mysql_init(NULL);

    char *sql = (char *)malloc(sizeof(char) * MAX_SQL_SIZE);
    memset(sql, 0, MAX_SQL_SIZE);

    const char *username = "root";
    const char *passwd = "1";
    const char *ip = "192.168.100.128";
    const char *db = "test";
    int port = 3306;

    // 1.1 设置断开连接之后自动重新连接
    int realconect = 1;
    mysql_options(mysql, MYSQL_OPT_RECONNECT, &realconect);

    // 1.2 设置连接超时时间
    int timeout = 5;
    mysql_options(mysql, MYSQL_OPT_CONNECT_TIMEOUT, &timeout);

    // 2、通过MYSQL句柄连接mysql server
    if (mysql_real_connect(mysql, ip, username, passwd, db, port, NULL, 0) == NULL)
    {
        printf("mysql real connect error:%s\n", mysql_error(mysql));
    }

    printf("mysql connect server Success!\n");
    strcpy(sql, "CREATE TABLE IF NOT EXISTS student3 (id INT AUTO_INCREMENT PRIMARY KEY, sname VARCHAR(50), sage INT)");
    mysql_real_query(mysql, sql, strlen(sql));

    MYSQL_STMT *smt = mysql_stmt_init(mysql);
    memset(sql, 0, MAX_SQL_SIZE);
    strcpy(sql, "INSERT INTO student3(sname, sage) VALUES(?, ?)");

    if (mysql_stmt_prepare(smt, sql, strlen(sql)) != 0)
    {
        fprintf(stderr, "mysql_stmt_prepare(INSERT) Error: %s\n", mysql_stmt_error(smt));
    }

    char sname[20] = "jsetc";
    int age = 5;
    MYSQL_BIND data[2];
    memset(data, 0, sizeof(data));
    data[0].buffer_type = MYSQL_TYPE_STRING;
    data[0].buffer_length = strlen(sname);
    data[1].buffer_type = MYSQL_TYPE_LONG;

    for (int i = 0; i < 100; i++)
    {
        age++;
        data[0].buffer = sname;
        data[1].buffer = &age;

        if (mysql_stmt_bind_param(smt, data) != 0)
        {
            fprintf(stderr, "mysql_stmt_bind_param(INSERT) Error: %s\n", mysql_stmt_error(smt));
        }

        if (mysql_stmt_execute(smt) != 0)
        {
            fprintf(stderr, "mysql_stmt_bind_param(INSERT) Error: %s\n", mysql_stmt_error(smt));
        }
    }

    MYSQL_STMT *select_stmt = mysql_stmt_init(mysql);

    memset(sql, 0, MAX_SQL_SIZE);
    strcpy(sql, "SELECT * FROM student3");

    if (mysql_stmt_prepare(select_stmt, sql, strlen(sql)) != 0)
    {
        fprintf(stderr, "mysql_stmt_prepare(SELECT) Error: %s\n", mysql_stmt_error(select_stmt));
    }

    if (mysql_stmt_execute(select_stmt) != 0)
    {
        fprintf(stderr, "mysql_stmt_execute(SELECT) Error: %s\n", mysql_stmt_error(select_stmt));
    }
    
    //设置模式：存储数据模式
    MYSQL_RES *result = mysql_stmt_result_metadata(select_stmt);
    if (!result) {
        fprintf(stderr, "mysql_stmt_result_metadata() failed\n");
        mysql_stmt_close(select_stmt);
        mysql_close(mysql);
        exit(1);
    }

    int len = mysql_num_fields(result);
    MYSQL_FIELD * field; 
    for(int i = 0; i < len; i++)
    {
       field = mysql_fetch_field(result);
       printf("%s|", field->name);
    }
    printf("\n");

    memset(sname, 0, sizeof(sname));
    age = 0;
    int id;
    long l_sname;

    MYSQL_BIND result_bind[3];
    memset(result_bind, 0, sizeof(result_bind));
   
    result_bind[0].buffer = &id;
    result_bind[0].buffer_type = MYSQL_TYPE_LONG;
   

    result_bind[1].buffer = sname;
    result_bind[1].buffer_type = MYSQL_TYPE_STRING;
    result_bind[1].buffer_length = sizeof(sname);
    result_bind[1].length = &l_sname;

    result_bind[2].buffer = &age;
    result_bind[2].buffer_type = MYSQL_TYPE_LONG;
    
    //绑定保存的结果
    if (mysql_stmt_bind_result(select_stmt, result_bind)) {
        fprintf(stderr, "mysql_stmt_bind_result() failed: %s\n", mysql_stmt_error(select_stmt));
        mysql_free_result(result);
        mysql_stmt_close(select_stmt);
        mysql_close(mysql);
        exit(1);
    }
    
    //获取所有结果数据：所有行
    if (mysql_stmt_store_result(select_stmt)) {
        fprintf(stderr, "mysql_stmt_store_result() failed: %s\n", mysql_stmt_error(select_stmt));
        mysql_free_result(result);
        mysql_stmt_close(select_stmt);
        mysql_close(mysql);
        exit(1);
    }

    while (mysql_stmt_fetch(select_stmt) == 0)
    {
        printf("%d | %s | %d\n", id, sname, age);
    }

    mysql_free_result(result);
    mysql_stmt_close(select_stmt);

    mysql_stmt_close(smt);

    mysql_close(mysql);

    return 0;
}